﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DBUtility;

namespace DAL
{
    public class GameMaster
    {
        public GameMaster()
        { }

        public bool GameMasterAdd(MODEL.GameMaster model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,20),
                                       new SqlParameter("@name",SqlDbType.VarChar,50),
                                       new SqlParameter("@register",SqlDbType.VarChar,100),
                                       new SqlParameter("@download",SqlDbType.VarChar,100),
                                       new SqlParameter("@url",SqlDbType.VarChar,100),
                                       new SqlParameter("@gamelogo",SqlDbType.VarChar,200),
                                       new SqlParameter("@company",SqlDbType.VarChar,100),
                                       new SqlParameter("@state",SqlDbType.Char,1),
                                       new SqlParameter("@money",SqlDbType.Int),
                                       new SqlParameter("@createtime",SqlDbType.DateTime),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = GetID();
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.GNAME;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = model.GREGISTER;
            parameter[2].Direction = ParameterDirection.Input;
            parameter[3].Value = model.GDOWNLOAD;
            parameter[3].Direction = ParameterDirection.Input;
            parameter[4].Value = model.GURL;
            parameter[4].Direction = ParameterDirection.Input;
            parameter[5].Value = model.GGAMELOGO;
            parameter[5].Direction = ParameterDirection.Input;
            parameter[6].Value = model.GCOMPANY;
            parameter[6].Direction = ParameterDirection.Input;
            parameter[7].Value = model.GSTATE;
            parameter[7].Direction = ParameterDirection.Input;
            parameter[8].Value = model.GMONEY;
            parameter[8].Direction = ParameterDirection.Input;
            parameter[9].Value = model.GCREATETIME;
            parameter[9].Direction = ParameterDirection.Input;
            parameter[10].Direction = ParameterDirection.ReturnValue;

            return DbHelper.RunProcedure("ProGamemasterAdd", parameter);
        }
        public bool GameMasterDel(string gId)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,20),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = gId;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Direction = ParameterDirection.ReturnValue;
            return DbHelper.RunProcedure("ProGamemasterDel", parameter);
        }
        public bool GameMasterUpdate(MODEL.GameMaster model)
        {
            SqlParameter[] parameter ={new SqlParameter("@id",SqlDbType.VarChar,20),
                                       new SqlParameter("@name",SqlDbType.VarChar,50),
                                       new SqlParameter("@register",SqlDbType.VarChar,100),
                                       new SqlParameter("@download",SqlDbType.VarChar,100),
                                       new SqlParameter("@url",SqlDbType.VarChar,100),
                                       new SqlParameter("@gamelogo",SqlDbType.VarChar,200),
                                       new SqlParameter("@company",SqlDbType.VarChar,100),
                                       new SqlParameter("@money",SqlDbType.Int),
                                       new SqlParameter("@Return",SqlDbType.Int)};
            parameter[0].Value = model.GID;
            parameter[0].Direction = ParameterDirection.Input;
            parameter[1].Value = model.GNAME;
            parameter[1].Direction = ParameterDirection.Input;
            parameter[2].Value = model.GREGISTER;
            parameter[2].Direction = ParameterDirection.Input;
            parameter[3].Value = model.GDOWNLOAD;
            parameter[3].Direction = ParameterDirection.Input;
            parameter[4].Value = model.GURL;
            parameter[4].Direction = ParameterDirection.Input;
            parameter[5].Value = model.GGAMELOGO;
            parameter[5].Direction = ParameterDirection.Input;
            parameter[6].Value = model.GCOMPANY;
            parameter[6].Direction = ParameterDirection.Input;
            parameter[7].Value = model.GMONEY;
            parameter[7].Direction = ParameterDirection.Input;
            parameter[8].Direction = ParameterDirection.ReturnValue;

            return DbHelper.RunProcedure("ProGamemasterUpdate", parameter);
        }
        public DataSet GameMasterList()
        {
            return DbHelper.RunProcedure("ProGamemasterSelect");
        }
        public DataSet GameMasterList(string gId)
        {
            SqlParameter[] parameter = { new SqlParameter("@id", SqlDbType.VarChar, 20) };
            parameter[0].Value = gId;
            parameter[0].Direction = ParameterDirection.Input;
            return DbHelper.RunProcedureS("ProGamemasterSelectByID", parameter);
        }
        public string GetID()
        {
            string returnStr = string.Empty;
            string id = DateTime.Now.ToString("yyyyMMdd");
            string sqlStr = "SELECT MAX(GID) FROM G_GAMEMASTER WHERE GID LIKE '" + id + "%'";
            DataSet ds = DbHelper.Query(sqlStr);
            if (ds.Tables[0].Rows[0][0].ToString() == "null" || ds.Tables[0].Rows[0][0].ToString() == null || ds.Tables[0].Rows[0][0].ToString() == "")
            {
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "001";
            }
            else
            {
                int i = Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString().Substring(17, 3));
                returnStr = DateTime.Now.ToString("yyyyMMddHHmmssfff") + string.Format("{0:D3}", i + 1);
            }
            return returnStr;
        }

    }
}
